<?php
namespace App\Http\Controllers\Admin;

use App\Http\Util\Err;
use App\Http\Util\HomeTool;
use App\Http\Util\Upload;
use App\Http\Controllers\Controller;
use App\Jobs\ThumbTask;
use App\Jobs\UploadTask;
use App\Models\Contract;
use App\Models\HandleFile;
use App\Models\NewSearch;
use App\Models\OldData;
use App\Models\OpenSearch;
use App\services\ImmService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Nette\Utils\FileSystem;
use OSS\OssClient;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;

class VideoUploadController extends Controller{

    /**测试数据
     * @param Request $request
     * @return void
     */
    public function convertData(Request $request){
        $date = date('Ymd',time());
        $config = [
            'accessId' => config('site.accessKeyId'),
            'accessKey' => config('site.accessSecret'),
            'endpoint' => config('site.point'),
            'bucket' => config('site.bucket')
        ];
//        $page = request()->get('page');
        $list = Contract::where('png_count',0)
            ->orderBy('id', 'asc')
            ->limit(100)
            ->get()->toArray();
//        foreach($list as $k => &$v){
//            $v['downpath'] = str_replace('下载地址1::::::','',$v['downpath']);
//            $v['downpath'] = str_replace('::::::1::::::0::::::','',$v['downpath']);
//            $v['alt'] = HomeTool::extract_img($v['newstext']);
//
//        }
//        $ossClient = new OssClient($config['accessId'], $config['accessKey'], $config['endpoint']);
//        try{
        foreach($list as $k => $v){
            $path = HomeTool::downloadfile($v['path'] , '/upload/temp');
            $pathArr = explode('.',  $path);
            $fileExt = end(  $pathArr  );
//            $doc_path = 'doc/'.$date.'/'.uniqid().'.'.$fileExt;
//            $result = $ossClient->uploadFile($config['bucket'],$doc_path, public_path($path));
//            $uid = uuid();
//            if ($result['info']) {
//                print_r( new \CURLFile(realpath(public_path($path))));exit;
                $post_data = [
                    'file' => new \CURLFile(realpath(public_path($path))),
                    'type' => $fileExt,
                    'outtype' => 1,
                    'callbackurl' => 'http://test.wendang369.com/api/handle_pic?uid='.$v['uid']
                ];
                $api = "http://all2img.ali.duhuitech.com/v1/convert_post";
                $header= 'aca9dd600912417f8f3ec7e02c101228';
                $rt = HomeTool::getCurl($api,$header,$post_data);
                $rt = json_decode($rt,true);
                if($rt['code'] != 10000)
                {
                    Log::info('file:'.$v['file'].'同步失败');
                }
                $save['convert_token'] = $rt['result']['token'];
                Contract::where('id',$v['id'])->update($save);
                Storage::disk('public_1')->delete($path);
            }
//        }
        return response()->json(['code'=>200]);
//        }catch (\Exception $e){
//            Log::info('失败原因:'.$e->getMessage());
//        }
    }

    public function saveUidData(){
        $list =Contract::where('png_count',0)
            ->where('convert_token','<>','')
            ->orderBy('id', 'asc')
            ->limit(50)
            ->get()->toArray();
        $url = 'https://api.duhuitech.com/q';
        $count = 0;
        foreach($list as $k => $v){
            $data['token'] = $v['convert_token'];
            $rt = httpGetRequest($url,$data);
            $rt = json_decode($rt,true);
            if($rt['code'] == 10000){
                $count ++;
                dispatch(new UploadTask($v['uid'],$rt))->onQueue('contract');
            }
        }
        echo $count;
    }

    public function test(){
//        $log = new Logger('wxpay');
//        $log->pushHandler(new StreamHandler(storage_path('logs/wxpay.log'),Logger::INFO) );
//        $log->notice(date('Y-m-d H:i:s').'-用户支付信息:');
        $model = Contract::where('is_make',3)->where('is_del',Err::DEL_SHOW)->limit(1000)->get()->toArray();
        $search = new NewSearch();
        $result = $search->add($model);
        var_dump($result);



//        HomeTool::downloadfile('https://www.ruiwen.com/word/gerenfangwumaimaihetongxieyishu.html' , '/upload/convert');
    }

}
